-- 24.lua
-- Created by zouyb Jun/26/2015
-- 强盗BUFF光效

return {
    onAppear = function(monster)    
	    -- 当前怪物的基类ID
		local baseClassId = monster:query("base_class_id");

        -- 获取其他所有怪物
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        
        -- 是否有酒鬼
        local needAddBuf = false;
        
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end
            
            -- 如果酒鬼在场，需要播放“属性增益”光效
            if monsterGrid.gridData.monster:query("base_class_id") == 27 then
                needAddBuf = true;
            end            
        end

        if needAddBuf then
            -- 酒鬼在场，播放“属性增益”光效
            local monsterGrid = UIDungeonMgr:getCurLevel().grids[monster:getPos()];
            playAddBuffEffect(monsterGrid, BUFF_TYPE_ATTACK);
        end

		-- 注册其他怪物死亡时的回调
		if count > 0 then
            UIDungeonMgr:getCurLevel().monsterBuffEffectM:addEventModule("monster24_appear");	
            EventMgr.removeAll("monster24_appear");	
    		EventMgr.register("monster24_appear", event.MONSTER_DIE_LOGIC, function(params)
    			local pos = params.pos;
    			local grid = DungeonM.getGridByPos(pos);
                if grid.monster then
    				local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
    				for i = 1, #openMonsterGrids do
    					local monsterGrid = openMonsterGrids[i];
    					-- 场上其他怪物死亡时，所有在场强盗播放“属性减益”光效
    					if monsterGrid.gridData.monster ~= grid.monster
    						and monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
    						playAddDeBuffEffect(monsterGrid, DEBUFF_TYPE_ATTACK);
    					end
    				end
    			end
    		end);
    	end			
    end,

    onDie = function(monster)   
        -- 当前怪物的基类ID
        local baseClassId = monster:query("base_class_id");

        -- 获取其他所有强盗
        local openMonsterGrids = UIDungeonMgr:getCurLevel():getOpenMonsterGrids();
        local count = 0;
        for i = 1, #openMonsterGrids do
            local monsterGrid = openMonsterGrids[i];
            if monsterGrid.gridData.monster:query("base_class_id") == baseClassId then
                count = count + 1;
            end
        end   
        
        if count == 0 then  
		    EventMgr.removeAll("monster24_appear");
		end  
    end,
};